//
//  JMyGlobeInfoModel.m
//  HeadIndustry
//
//  Created by jun on 2017/9/1.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JMyGlobeInfoModel.h"

static JMyGlobeInfoModel *g_Data = nil;

@implementation JMyGlobeInfoModel

+ (instancetype)shareInstance {
    @synchronized(self) {
        if (g_Data == nil) {
            g_Data = [[JMyGlobeInfoModel alloc] init];
        }
    }
    return g_Data;
}

- (id)init {
    if (self = [super init]) {
        _toShopCarModel = [JAddToShopCarRequestModel new]; // 购买物品信息参数
        _toShopCarModel.CustomerId = _userInfoModel.CustomerId;
        
        _userInfoModel = [JUserInfoModel new]; // 暂时代替用户ID
        _userInfoModel.CustomerId = 9616;
//        _userInfoModel.CustomerId = 10004; // 780
        
        
    }
    
    return self;
}

#pragma mark - 缓存数据
#define USERINFO_CACHE                 @"USERINFO_CACHE.txt"

- (void)setUserInfoModel:(JUserInfoModel *)userInfoModel
{
    _userInfoModel = userInfoModel;
    _token_Id = [NSString stringWithFormat:@"token=%@&customerId=%ld",userInfoModel.Unionid,userInfoModel.Id];
    _userInfoModel.CustomerId = _userInfoModel.Id;
}

//恢复数据
- (void)restoreData {
    @try {
        @autoreleasepool {
            //
        }
        
        NSString *filePath = nil;
//        NSMutableArray *temp = nil;
        
        filePath = [self createlistWithName:USERINFO_CACHE];
        JUserInfoModel *tempUserInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (tempUserInfo != nil) {
            self.userInfoModel = tempUserInfo;
            tempUserInfo = nil;
        }
        
//        filePath = [self createlistWithName:LOGINNAME_CACHE];
//        NSString *tempLoginName = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (tempLoginName != nil && ![tempLoginName isEqualToString:@""]) {
//            self.strLoginName = tempLoginName;
//            tempLoginName = nil;
//        }
//        
//        filePath = [self createlistWithName:CITY_CACHE];
//        City *tempCity = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (tempCity != nil) {
//            self.objCity = tempCity;
//            tempCity = nil;
//        }
//        
//        filePath = [self createlistWithName:ADVERT_CACHE];
//        Advert *tempAdvert = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (tempAdvert != nil) {
//            self.objAdvert = tempAdvert;
//            tempAdvert = nil;
//        }
//        
//        filePath = [self createlistWithName:NOFINISHORDER_CACHE];
//        NSString *tempNoFinishOrder = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (tempNoFinishOrder != nil && ![tempNoFinishOrder isEqualToString:@""]) {
//            self.strNoFinishOrder = tempNoFinishOrder;
//            tempLoginName = nil;
//        }
//        
//        filePath = [self createlistWithName:CARBRAND_CACHE];
//        temp = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (temp != nil) {
//            self.mArrCarBrand = temp;
//            temp = nil;
//        }
//        
//        filePath = [self createlistWithName:MYCAR_CACHE];
//        temp = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (temp != nil) {
//            self.mArrMyCar = temp;
//            temp = nil;
//        }
//        
//        filePath = [self createlistWithName:SEARCHDESTINATIONHISTORY_CACHE];
//        temp = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (temp != nil) {
//            self.mArrSearchDestinationHistory = temp;
//            temp = nil;
//        }
//        
//        filePath = [self createlistWithName:LASTCOORDINATE_CACHE];
//        SearchMapResult *tempLastCoordinate = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//        if (tempLastCoordinate != nil) {
//            self.objLastCoordinate = tempLastCoordinate;
//            tempLastCoordinate = nil;
//        }
        
    }
    @catch (NSException * e) {
        
    }
    @finally {
        
    }
}

//保存数据
- (void)saveData {
    @try {
        NSString * filePath = nil;
        
        filePath = [self createlistWithName:USERINFO_CACHE];
        if (self.userInfoModel) {
            [NSKeyedArchiver archiveRootObject:self.userInfoModel toFile:filePath];
        }
        
        
//        filePath = [self createlistWithName:LOGINNAME_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.strLoginName toFile:filePath];
//        
//        filePath = [self createlistWithName:CITY_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.objCity toFile:filePath];
//        
//        filePath = [self createlistWithName:ADVERT_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.objAdvert toFile:filePath];
//        
//        filePath = [self createlistWithName:NOFINISHORDER_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.strNoFinishOrder toFile:filePath];
//        
//        filePath = [self createlistWithName:CARBRAND_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.mArrCarBrand toFile:filePath];
//        
//        filePath = [self createlistWithName:MYCAR_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.mArrMyCar toFile:filePath];
//        
//        filePath = [self createlistWithName:SEARCHDESTINATIONHISTORY_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.mArrSearchDestinationHistory toFile:filePath];
//        
//        filePath = [self createlistWithName:LASTCOORDINATE_CACHE];
//        [NSKeyedArchiver archiveRootObject:self.objLastCoordinate toFile:filePath];
        
    }
    @catch (NSException * e) {
        //
    }
    @finally {
        //
    }
}

- (NSString * )createlistWithName:(NSString *)name {
    NSFileManager * manager = [NSFileManager defaultManager];
    NSString * pre = [self DocPath];
    NSString * fullPath = [NSString stringWithFormat:@"%@/contents/",pre];
    
    BOOL dir = 1;
    
    if (![manager fileExistsAtPath:fullPath isDirectory:&dir]) {
        [manager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    fullPath = [fullPath stringByAppendingString:name];
    dir = 0;
    
    if (![manager fileExistsAtPath:fullPath isDirectory:&dir]) {
        [manager createFileAtPath:fullPath contents:nil attributes:nil];
    }
    
    return fullPath;
}

- (NSString *) DocPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

@end
